package com.lagou.subjectthree;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SubjectThreeMain {
    public static void main(String[] args) {
        //创建一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //定义路径
        String path = "../test";
        String pathCopy = "../testCopy";
        System.out.println("复制路径：" + path + "所有文件,至" + pathCopy);
        File file = new File(path);
        File fileCopy = new File(pathCopy);
        CreateFile(fileCopy);
        //调用方法
        CopyAllFile(file, fileCopy, executorService);


        //关闭线程池
        executorService.shutdown();
    }

    /**
     * 复制所有的文件
     *
     * @param file
     * @param executorService
     */
    public static void CopyAllFile(File file, File fileCopy, ExecutorService executorService) {
        //判断被复制的文件是不是存在
        if (!file.exists()) {
            return;
        }
        //存在进行复制
        //复制的文件的路径
        File copy = new File(fileCopy.getPath() + "\\" + file.getName());
        System.out.println("复制：" + file.getPath() + "  到：" + fileCopy.getPath() + "\\" + file.getName());
        //是不是文件夹
        if (!file.isDirectory()) {
            //复制文件，向线程池中添加布置任务
            executorService.submit(new CopyFileThread(file, copy));
        } else {
            //复制文件夹
            CreateFile(copy);
            //复制文件夹底下所有文件
            File[] files = file.listFiles();
            for (File f : files) {
                CopyAllFile(f, copy, executorService);
            }
        }
    }

    /**
     * 创建文件夹底下所有文件！
     * @param fileCopy
     */
    public static void CreateFile(File fileCopy) {
        if (!fileCopy.exists()) {
            System.out.println("创建文件夹" + fileCopy.getPath());
            fileCopy.mkdir();
        }
    }
}
